package org.urbansoft.icl.record;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.urbansoft.icl.field.Field;
import org.urbansoft.icl.field.GenericAField;
import org.urbansoft.icl.field.GenericANField;
import org.urbansoft.icl.field.GenericBlankField;
import org.urbansoft.icl.field.GenericNField;
import org.urbansoft.icl.field.Position;
import org.urbansoft.icl.field.Usage;
import org.urbansoft.icl.field.ValidationCriteria;
import org.urbansoft.icl.field.validation.RecordTypeValidator;
import org.urbansoft.icl.util.EasternStandardTime;

/**
 * The Class CashLetterControlRecord.
 */
public class CashLetterControlRecord extends Record {
	public Field recordType;
	public Field bundleCount;
	public Field itemCount;
	public Field cashLetterAmount;
	public Field imagesWithinCashLetterCount;
	public Field eceInstitutionName;
	public Field settlementDate;
	public Field reserved;

	private static Logger log = Logger.getLogger(CashLetterControlRecord.class);

	/**
	 * Instantiates a new cash letter control record.
	 */
	public CashLetterControlRecord() {
		fields = new ArrayList<Field>();
		recordName = "Cash Letter Control Record";
		recordTypeNumber = "90";
		initFields();
	}

	/**
	 * Instantiates a new cash letter control record.
	 */
	public CashLetterControlRecord(String read) {
		// log.info("New CashLetterControlRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "Cash Letter Control Record";
		recordTypeNumber = "90";
		initFields2();
	}

	/**
	 * Instantiates a new cash letter control record.
	 */
	public CashLetterControlRecord(int read) {
		// log.info("New CashLetterControlRecord for WRITE");
		fields = new ArrayList<Field>();
		recordName = "Cash Letter Control Record";
		recordTypeNumber = "90";
		initFields3();
	}

	/**
	 * Inits the fields.
	 */
	private void initFields2() {
		try {
			/* check detail record */
			recordType = new GenericANField(new Position(1, 2), "Record Type");
			/* bundle count */
			bundleCount = new GenericANField(new Position(3, 8), "Bundle Count");

			/* item count */
			itemCount = new GenericANField(new Position(9, 16),
					"Items Within Cash Letter Count");

			/* letter amount */
			cashLetterAmount = new GenericANField(new Position(17, 35),
					"Cash Letter Total Amount");

			/* images in cash letter count */
			imagesWithinCashLetterCount = new GenericANField(new Position(36,
					44), "Images Within Cash Ltter Count");

			/* ECE institution name */
			eceInstitutionName = new GenericANField(new Position(45, 62),
					"ECE Institution Name");

			/* settlement date */
			settlementDate = new GenericANField(new Position(63, 70),
					"Settlement Date");

			/* Reserved */
			reserved = new GenericANField(new Position(71, 90),
					"Reserved Field");

			fields.add(recordType);
			fields.add(bundleCount);
			fields.add(itemCount);
			fields.add(cashLetterAmount);
			fields.add(imagesWithinCashLetterCount);
			fields.add(eceInstitutionName);
			fields.add(settlementDate);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields3() {
		try {
			/* check detail record */
			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "90", "Record Type");
			/* bundle count */
			bundleCount = new GenericANField(new Position(3, 8), "Bundle Count");
			bundleCount.setFieldData("");

			/* item count */
			itemCount = new GenericANField(new Position(9, 16),
					"Items Within Cash Letter Count");
			itemCount.setFieldData("");

			/* letter amount */
			cashLetterAmount = new GenericANField(new Position(17, 35),
					"Cash Letter Total Amount");
			cashLetterAmount.setFieldData("");

			/* images in cash letter count */
			imagesWithinCashLetterCount = new GenericANField(new Position(36,
					44), "Images Within Cash Ltter Count");
			imagesWithinCashLetterCount.setFieldData("");

			/* ECE institution name */
			eceInstitutionName = new GenericANField(new Position(45, 62),
					"ECE Institution Name");
			eceInstitutionName.setFieldData("");

			/* settlement date */
			settlementDate = new GenericANField(new Position(63, 70),
					"Settlement Date");
			settlementDate.setFieldData(EasternStandardTime
					.getCurrentDateInEST());

			/* Reserved */
			reserved = new GenericANField(new Position(71, 90),
					"Reserved Field");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(bundleCount);
			fields.add(itemCount);
			fields.add(cashLetterAmount);
			fields.add(imagesWithinCashLetterCount);
			fields.add(eceInstitutionName);
			fields.add(settlementDate);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields() {
		try {
			/* check detail record */
			recordType = new GenericNField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "90", "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* bundle count */
			bundleCount = new GenericNField(new Position(3, 8), "Bundle Count");

			/* item count */
			itemCount = new GenericNField(new Position(9, 16),
					"Items Within Cash Letter Count");

			/* letter amount */
			cashLetterAmount = new GenericNField(new Position(17, 30),
					"Cash Letter Total Amount");

			/* images in cash letter count */
			imagesWithinCashLetterCount = new GenericNField(
					new Position(31, 39), "Images Within Cash Ltter Count");
			imagesWithinCashLetterCount.setUsage(Usage.CONDITIONAL);

			/* ECE institution name */
			eceInstitutionName = new GenericAField(new Position(40, 57),
					"ECE Institution Name");
			eceInstitutionName.setUsage(Usage.CONDITIONAL);

			/* settlement date */
			settlementDate = new GenericNField(new Position(58, 65),
					"Settlement Date");
			settlementDate.setUsage(Usage.CONDITIONAL);

			/* Reserved */
			reserved = new GenericBlankField(new Position(66, 80),
					"Reserved Field");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(bundleCount);
			fields.add(itemCount);
			fields.add(cashLetterAmount);
			fields.add(imagesWithinCashLetterCount);
			fields.add(eceInstitutionName);
			fields.add(settlementDate);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

}
